package com.lockapp;

import com.lockapp.database.BlockCursor;
import com.lockapp.database.BlockDAO;
import com.lockapp.database.SenhaCursor;
import com.lockapp.database.SenhaDAO;
import com.lockapp.entity.Block;
import com.lockapp.entity.Senha;
import com.lockapp.services.LockService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;

public class SenhaActivity extends Activity{
	
	private EditText digitoUm;
	private EditText digitoDois;
	private EditText digitoTres;
	private EditText digitoQuatro;
	private int idBlockParametro;
	private SenhaDAO dao;
	private SenhaCursor senhaCursor;
	private BlockDAO daoBlock;
	private BlockCursor blockCursor;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_senha);
//		stopService(new Intent(this, LockService.class));
		
		digitoUm = (EditText) findViewById(R.id.editText1);
		digitoDois = (EditText) findViewById(R.id.editText2);
		digitoTres = (EditText) findViewById(R.id.editText3);
		digitoQuatro = (EditText) findViewById(R.id.editText4);	
		
		dao = new SenhaDAO(getApplicationContext());
		daoBlock = new BlockDAO(getApplicationContext());
		
		
		Intent intent = getIntent();
		idBlockParametro = intent.getExtras().getInt("block");
		
		digitoUm.requestFocus();
		digitoUm.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoUm.getText().toString().length() > 0){
					digitoDois.requestFocus();
				}
				return false;
			}
		});
		
		digitoDois.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoDois.getText().toString().length() > 0){
					digitoTres.requestFocus();
				}
				return false;
			}
		});
		
		digitoTres.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoTres.getText().toString().length() > 0){
					digitoQuatro.requestFocus();
				}
				return false;
			}
		});
		
		digitoQuatro.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(digitoQuatro.getText().toString().length() > 0){
					Senha senha = new Senha();
					senha.setDigitoUm(Integer.parseInt(digitoUm.getText().toString()));
					senha.setDigitoDois(Integer.parseInt(digitoDois.getText().toString()));
					senha.setDigitoTres(Integer.parseInt(digitoTres.getText().toString()));
					senha.setDigitoQuatro(Integer.parseInt(digitoQuatro.getText().toString()));
					
					senhaCursor = dao.getSenhas();
					if(senhaCursor != null || senhaCursor.getCount() != 0){
						for (int i = 0; i < senhaCursor.getCount(); i++) {
							Senha senhaBanco = new Senha();
							senhaBanco.setDigitoUm(senhaCursor.getDigitoUm());
							senhaBanco.setDigitoDois(senhaCursor.getDigitoDois());
							senhaBanco.setDigitoTres(senhaCursor.getDigitoTres());
							senhaBanco.setDigitoQuatro(senhaCursor.getDigitoQuatro());
							if(senha.getDigitoUm() == senhaBanco.getDigitoUm() && senha.getDigitoDois() == senhaBanco.getDigitoDois() && senha.getDigitoTres() == senhaBanco.getDigitoTres() && senha.getDigitoQuatro() == senhaBanco.getDigitoQuatro()){
								blockCursor = daoBlock.getBlockId(idBlockParametro);
								Block blockAlterar = new Block();
								blockAlterar.setIdBlock(blockCursor.getIdBlock());
								blockAlterar.setLiberacao(1);
								daoBlock.alterar(blockAlterar);
								daoBlock.getAll();
								finish();
								break;
							}
						}
						
					}
				}
				return false;
			}
		});
		
	};
	
}
